package com.dily.study.demo.thread;

import java.util.concurrent.*;

/**
 * Date: 2022-08-09 星期二
 * Time: 16:28
 * Author: Dily_Su
 * Remark:
 * 通过实现 Callable 创建线程
 * 创建线程池 提交任务用于执行任务
 */
public class TestCallable implements Callable<Boolean> {

    @Override
    public Boolean call() throws Exception {
        for (int i = 0; i<20; i++){
            System.out.println( Thread.currentThread().getName()+ "我在学习多线程：" + i);
        }
        return true;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        TestCallable t1 = new TestCallable();
        TestCallable t2 = new TestCallable();
        TestCallable t3 = new TestCallable();


        // 创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(3);

        // 提交执行
        Future<Boolean> r1 = executorService.submit(t1);
        Future<Boolean> r2 = executorService.submit(t2);
        Future<Boolean> r3 = executorService.submit(t3);

        // 获取结果
        boolean rs1 = r1.get();
        boolean rs2 = r2.get();
        boolean rs3 = r3.get();

        executorService.shutdown();
    }
}
